package org.steven.crm.core.service.systemuser;

import org.steven.crm.core.service.BaseService;
import org.steven.crm.core.utils.TreeUtil;
import com.steven.xdb.dbmanager.DBManager;
import com.steven.xdb.pagination.query.AbstractQueryDataList;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author wuzw
 * @Date 2023/5/16 15:27
 * @Version 1.0
 */
public class SystemMenuService extends BaseService {

    @Override
    protected String getTable() {
        return "systemMenu";
    }

    @Override
    protected AbstractQueryDataList getQueryDataList() {
        return null;
    }

    @Override
    protected String getDataSourceName() {
        return null;
    }

    public List<Map<String, String>> readAllValid() throws Exception {
        Map<String, String> k = new HashMap<>();
        k.put("validFlag", "1");

        String extendSql = " order by sortIndex";
        return DBManager.query(getSession(), getTable(), "*", k, extendSql);
    }

    public List<String> readAllValidIds() throws Exception {
        List<Map<String, String>> datas = readAllValid();
        List<String> ids = new ArrayList<>();
        for (Map<String, String> data : datas) {
            ids.add(data.get("id"));
        }

        return ids;
    }

    public List<Map<String, String>> readValidDataByIds(Collection<String> ids) throws Exception {
        if (ids == null || ids.size() == 0) {
            return new ArrayList<>();
        }
        String sql = "select * from " + getTable() + " where id in ("
                + ids.stream().collect(Collectors.joining(","))
                + ") and validFlag = '1'";
        return DBManager.query(getSession(), sql, new ArrayList<>());
    }

    public List<Map<String, Object>> getMenuTree() throws Exception {
        List<Map<String, String>> menus = readAllValid();

        return TreeUtil.makeTree(menus, "parentId");
    }
}
